using System;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Configuration;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace CKS.FormsBasedAuthentication
{   
    /// <summary>
    /// This class is a feature activation event handler for the Admin Management feature. It (de)activates/(un)installs all the sub features of related to FBA Admin Management
    /// </summary>
    class AdminManagementFeatureReceiver : SPFeatureReceiver
    {
        private Guid _solutionID = new Guid("EC69235D-7FA1-4382-B7F8-0E7029DAEDA9");
        private Dictionary<String, Guid> _features = new Dictionary<String, Guid>();

        public AdminManagementFeatureReceiver()
        {
            _features.Add("UserAdministration", new Guid("C990A4C5-C5D9-474b-B6F6-67329F1E6A0D")); // user administration   
            _features.Add("SiteConfig", new Guid("351D00CC-A155-4323-A432-4E474586CFB9"));   //site config                                          
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // get site reference
            SPSite site = properties.Feature.Parent as SPSite;

            /* bms Get a reference to the web */
            // get a web reference
            SPWeb web = site.OpenWeb(); 
            /* bms Set the URL strings in the web properties */
            //TODO: Make these resources
            UpdateProperty(MembershipReviewSiteURL.CHANGEPASSWORDPAGE,"Pages/ChangePassword.aspx",web);
            UpdateProperty(MembershipReviewSiteURL.PASSWORDQUESTIONPAGE,"Pages/PasswordQuestion.aspx",web);
            UpdateProperty(MembershipReviewSiteURL.THANKYOUPAGE,"Pages/Thankyou.aspx",web);

            /* bms Set the XSLT location web properties */
            //TODO: Make these resources

            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPAPPROVED,"/_layouts/FBA/emails/MembershipApproved.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPERROR,"/_layouts/FBA/emails/MembershipError.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPPENDING,"/_layouts/FBA/emails/MembershipPending.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPREJECTED,"/_layouts/FBA/emails/MembershipRejected.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.PASSWORDRECOVERY,"/_layouts/FBA/emails/PasswordRecovery.xslt",web);

            // activate sub features            
            bool activate = true;
            Utils.ToggleFeatureActivation(site, _features, activate);

            // update layouts bread crumb
            this.UpdateLayoutSiteMap(site);

            //TODO: Make sure we are suppose to call Dispose because we didn't create a new object
            site.Dispose();
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            // get site reference
            SPSite site = properties.Feature.Parent as SPSite;

            // deactivate sub features
            bool activate = false;
            Utils.ToggleFeatureActivation(site, _features, activate);

            //TODO: Make sure we are suppose to call Dispose because we didn't create a new object
            site.Dispose();
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) 
        {
            // install sub features
            bool install = true;
            Utils.ToggleFeatureInstallation(_features, _solutionID, install);                         
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) 
        {
            // uninstall sub features
            bool install = false;
            Utils.ToggleFeatureInstallation(_features, _solutionID, install);
        }

        private void UpdateLayoutSiteMap(SPSite site)
        {
            // update layouts site map
            try
            {
                UpdateLayoutsSitemap uls = new UpdateLayoutsSitemap(site.WebApplication);
                uls.AddSitemap("layouts.sitemap.FBASelfManagement.xml");
                uls.SubmitJob();
            }
            catch (Exception ex)
            {
                Utils.LogError(ex);
            }
        }

        private void UpdateProperty(string key, string value, SPWeb currentWeb)
        {
            if (currentWeb.Properties.ContainsKey(key))
            {
                currentWeb.Properties[key] = value;
            }
            else
            {
                currentWeb.Properties.Add(key, value);

            }
            currentWeb.Properties.Update();
        }
    }
}
